﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ToolsCXml
{
    public class CItem
    {
        public CItem( )
        {
            htmltxt = "";
        }
        public virtual  CItem MoveNext()
        {
            htmltxt = "";
            return this;
        }
        public int ID { get; set; }
        public string Url { get; set; }
        //public string FileName { get; set; }

        public string Text {
            get
            {
                if (htmltxt == "")
                {
                    if (Url != null)
                        htmltxt = web.GetOKUrl(Url);
                    else
                        htmltxt = "";
                }
                return htmltxt;
            }
        }
        public void SetHtml(string txt){
        	htmltxt = txt;
        }
        protected string htmltxt;
        public  static CWeb web = new CWeb();
    }
    public class ItemNetNumber : CItem
    {
        public ItemNetNumber(string Url, string UrlTemplate, BEId Beid)
        {
            this.Url = Url;
            this.UrlTemplate = UrlTemplate;
            this.Beid = Beid;
            ID = Beid.ActiveIndex;
        }
        public override CItem MoveNext()
        {
            Beid.MoveNext();
            if (Beid.HasNext)
            {
                Url = UrlTemplate.Replace("(*)", Beid.ActiveIndex.ToString());
                ID = Beid.ActiveIndex;
                htmltxt = "";
                return this;
            }
            return null;
        }
        private BEId Beid;
        private string UrlTemplate;
    }
    public class ItemNetNextUrl : CItem
    {
        public ItemNetNextUrl(string Url, string UrlTemplate, BETags NextUrlTags)
        {
            ID = 0;
            this.Url = Url;
            this.UrlTemplate = UrlTemplate;
            this.NextUrlTags = NextUrlTags;
        }
        public override CItem MoveNext()
        {
            string nexturl = ComputeNextUrl(htmltxt);
            if (nexturl != null)
            {
                Url = nexturl;
                ID++;
                htmltxt = "";
                return this;
            }              
            return null;
        }
        private string ComputeNextUrl(string txt)
        {
            string url = NextUrlTags.Match(txt);
            if (DgvTools.ValidUrl(url))
            {
                if (url.Contains("/"))
                {
                    string[] paths = url.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string s in paths)
                    {
                        if (UrlTemplate.Contains(s))
                        {
                            return UrlTemplate.Substring(0, UrlTemplate.LastIndexOf("/" + s)) + url;                          
                        }
                    }
                }
            }
            return null;
        }
        private string UrlTemplate;
        private BETags NextUrlTags;
    }
}
